// JavaScript Document

var paging = paging ||{};
(function(paging){
	var getParam = function(event){
		event = event||window.event;
		var target = event.target;
		var param;
		var params = paging.defaults.params;
		for(var i =0;i<params.length;i++){
			if(params[i].previou==target||params[i].next==target||params[i].current==target){
				param = params[i];
				break;
			}
		}
		return param;
	}
	var changeInfo = function(event,len){
		var param = getParam(event);

		var data = param.data||[];
		
		var total = param.total;
		var index = param.index;
		if(index+len>total-1||index+len<0){
			return;
		}
		var size = param.size;
		index = param.index = param.index+len;
		
		var current = param.current;
		//current.innerHTML = param.index+1+"/"+ param.total;
		current.value = param.index+1;
		
		var d = data.slice(index*size,(index+1)*size);
		param.callBack(d);
	}
	var loadInfo = function(event,len){
		var param = getParam(event);
		
		var data = param.data||[];
		var total = param.total;
		var index = param.index;
		if(index+len>total-1||index+len<0){
			return;
		}
		var size = param.size;
		var url = param.url;
		if(url.indexOf("?")<0){
			url = url + "?1=1";
		}
		//var url = buildUrl(param);
		index = param.index = param.index+len;
		var myajax = getByAjax();
		myajax.init(null,"get",true);
		myajax.get(url+"&index="+index+"&size="+size,function(d){
			//var index = params.index;
			//var size = param.size;
			var current = param.current;
			//current.innerHTML = param.index+1+"/"+ param.total;
			current.value = param.index+1;
			param.callBack(toJson(d));
			param = null;
		});
		myajax = null;
	}
	var skip = function(event,len){
		var param = getParam(event);
		var index = param.index;
		
		var total = param.total;
		if(len>total||len<1||len-1==index){
			return;
		}
		var size = param.size;
		index = param.index = len-1;
		var ajaxLoad = param.ajaxLoad;
		if(ajaxLoad){
			var url = param.url;
			if(url.indexOf("?")<0){
				url = url + "?1=1";
			}
			//var url = buildUrl(param);
			var myajax = getByAjax();
			myajax.init(null,"get",true);
			myajax.get(url+"&index="+index+"&size="+size,function(d){
				//var index = params.index;
				//var size = param.size;
				var current = param.current;
				current.innerHTML = param.index+1+"/"+ param.total;
				param.callBack(toJson(d));
				param = null;
			});
			myajax = null;
		}
		else{
			var data = param.data;
			var d = data.slice(index*size,(index+1)*size);
			param.callBack(d);
		}
	}
	var init = function(param){
		//param.ajaxLoad = param.ajaxLoad||true;
		if(param.ajaxLoad == undefined){
			param.ajaxLoad = true;
		}
		param.index = param.index||0;
		param.size = param.size||10;
		param.data = param.data||{};
		paging.defaults.params.push(param);
		
		var previou = param.previou;
		var next = param.next;
		var current = param.current;
		//param.current.innerHTML = "1/"+ param.total;
		param.max.innerHTML = "共 "+ param.total+" 页";
		
		previou.onclick = function(event){
			if(param.ajaxLoad){
				loadInfo(event,-1);
			}
			else{
				changeInfo(event,-1);
			}
		}
		next.onclick = function(event){
			if(param.ajaxLoad){
				loadInfo(event,1);
			}
			else{
				changeInfo(event,1);
			}
		}
		current.onblur = function(event){
			event = event||window.event;
			var target = event.target;
			var len = target.value;
			skip(event,len);
		}
	}
	var setUrl = function(index,url){
		var param = paging.defaults.params[index];
		if(!param){
			return;
		}
		param.url = url;
		param.index = 0;
		param.current.innerHTML = "1/"+param.total;
	}
	paging.page = function(param){
		init(param);
		//paging();
	}	
	paging.setUrl = function(index,url){
		setUrl(index,url);
		//param.total = total||param.total;
	}
	paging.defaults={
		params:[],
		/*index:0,
		size:10,
		total:1,
		url:"",
		ajaxLoad:true,//默认为异步获取数据
		previou:null,
		next:null,
		current:null,
		callBack:null,*/
	}
})(paging);